﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using studentskicentarETFa;
using MySql.Data;
using System.Data;
using MySql.Data.MySqlClient;
namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for DodavanjeStudenta.xaml
    /// </summary>
    public partial class DodavanjeStudenta : Window
    {
        private DataGrid PregledClanovaGrid;

        public DodavanjeStudenta()
        {
            InitializeComponent();
        }

        public DodavanjeStudenta(DataGrid PregledClanovaGrid)
        {
            InitializeComponent();
            this.PregledClanovaGrid = PregledClanovaGrid;
        }


        private void OtkaziStudent_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void PotvrdiStudent_Click(object sender, RoutedEventArgs e)
        {
            if (IDTextBoxStudent.Text == "" || IDKarticeTextBoxStudent.Text == "" || ImeTextBoxStudent.Text == "" || PrezimeTextBoxStudent.Text == "" || BrojIndexaTextBoxStudent.Text == "" || TelefonTextBoxStudent.Text == "")
            {
                System.Windows.Forms.MessageBox.Show("Unesite sve potrebne podatke!", "Upozorenje");
            }

            else
            {
                Student st = new Student(IDTextBoxStudent.Text, IDKarticeTextBoxStudent.Text, ImeTextBoxStudent.Text, PrezimeTextBoxStudent.Text, Convert.ToInt32(BrojIndexaTextBoxStudent.Text), TelefonTextBoxStudent.Text/*, Convert.ToDateTime(DatumRegStudent.SelectedDate)*/);
                studentskicentarETFa.DAL d = studentskicentarETFa.DAL.Instanca;
                d.kreirajKonekciju("localhost", "Centar", "root", "");
                studentskicentarETFa.DAL.StudentDAO s = new studentskicentarETFa.DAL.StudentDAO();
                List<Student> lista = new List<Student>();
                lista = s.getAll();
                bool da = false;
                foreach (Student stud in lista)
                {
                    if (stud.ID_Clana1 == IDTextBoxStudent.Text || stud.ID_Kartice1==IDKarticeTextBoxStudent.Text)
                    {
                        da = true;
                        break;
                    }
                }
                if (da) System.Windows.Forms.MessageBox.Show("Vec postojeci ID");
                else
                {
                    s.create(st);
                    System.Windows.MessageBox.Show("Uspjesno ste dodali novog clana.");
                    BindMyDataStudent();
                }
            }

        }

        private void BindMyDataStudent()
        {
            MySqlConnection con = null;
            string connectionString = "server=localhost;user=root;pwd=;database=centar";
            con = new MySqlConnection(connectionString);
            try
            {
                con.Open();
                MySqlCommand comm = new MySqlCommand("SELECT * FROM Student", con);
                comm.CommandText = "SELECT * from Student";
                DataSet ds = new DataSet();
                MySqlDataAdapter da = new MySqlDataAdapter(comm);
                da.Fill(ds);
                PregledClanovaGrid.ItemsSource = ds.Tables[0].DefaultView;
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message.ToString());
            }
            finally
            {
                con.Close();
            }
        }

     
    }
}
